<?php

namespace app\admin\controller;

use app\common\baidu\Trans;
use app\common\entity\BackResult;
use app\common\exception\BackErrorException;
use app\common\service\UploadService;
use app\common\service\VideoClaimService;
use app\common\service\VideoService;
use app\Request;

/**
 * 视频管理
 */
class LocalVideo extends Base
{

    public function one(Request $request)
    {
        $id = $request->param("id");
        $service = new VideoService();
        $video = $service->getOne($id);
        return BackResult::success($video, '获取成功');
    }

    /**
     * 获取单词列表
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getVideoWord(Request $request)
    {
        $page = $request->param("page", 1);
        $pageSize = $request->param('pageSize', 10);
        $videoId = $request->param('video_id');
        $data = (new VideoService())->getVideoWordList($page, $pageSize, $videoId);
        return BackResult::success($data);
    }

    /**
     * 给视频添加单词
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addWord(Request $request)
    {
        $params = $request->param();
        $data = (new VideoService())->addVideoWord($params);
        return BackResult::success($data);
    }

    /**
     * 获取全部单词
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function wordList(Request $request)
    {
        $page = $request->param("page", 1);
        $pageSize = $request->param('pageSize', 10);
        $data = (new VideoService())->getWordList($page, $pageSize);
        return BackResult::success($data);
    }

    /**
     * 获取认领视频列表
     * @param Request $request
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $page = $request->param("page", 1);
        $pageSize = $request->param('pageSize', 10);
        $searchField = $request->param('search_field', '');
        $searchValue = $request->param('search_value', '');
        $status = $request->param('delete_time', 0);
        $service = new VideoService();
        $data = $service->getListByRole($page, $pageSize, $this->admin, $searchField, $searchValue, $status);
        return BackResult::success($data);
    }

    /**
     * 获取未认领列表
     * @param Request $request
     * @return \think\Response
     */
    public function getNoClaim(Request $request)
    {
        $page = $request->param("page", 1);
        $pageSize = $request->param('pageSize', 10);
        $service = new VideoService();
        $data = $service->getNoClaimVideo($page, $pageSize, $this->admin);
        return BackResult::success($data);
    }

    /**
     * 认领视频
     * @param Request $request
     * @return \think\Response
     */
    public function toClaimVideo(Request $request)
    {
        $params = $request->param();
        if (empty($params['video_id'])) {
            return BackResult::error([], "参数错误");
        }
        $res = (new VideoClaimService())->insertClaimVideo($params['video_id'], $this->admin['id']);
        return BackResult::success($res, '认领成功');
    }

    /**
     * 放弃认领
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function abandonVideo(Request $request)
    {
        $params = $request->param();
        if (empty($params['video_id'])) {
            return BackResult::error([], "参数错误");
        }
        $res = (new VideoClaimService())->abandonVideoId($params['video_id'], $this->admin);
        return BackResult::success($res);
    }

    /**
     * 创建视频分类
     * @param Request $request
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $data = $request->post();
        $service = new VideoService();
        $data = $service->create($data);
        return BackResult::success($data);
    }

    /**
     * 更新视频
     * @param Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $data = $request->post();
        $id = $request->param("id");
        if (empty($id)) {
            return BackResult::error([], "缺少id");
        }
        $service = new VideoService();
        $data = $service->update($data, $id);
        return BackResult::success($data);
    }

    /**
     * 上架视频
     * @param Request $request
     * @return \think\Response
     */
    public function shelves(Request $request)
    {
        $id = $request->param("id");
        if (empty($id)) {
            return BackResult::error([], "缺少id");
        }
        $data = (new VideoService())->shelvesVideo($id);
        return BackResult::success($data);
    }

    /**
     * 下架视频
     * @param Request $request
     * @return \think\Response
     */
    public function remove(Request $request)
    {
        $id = $request->param('id');
        $service = new VideoService();
        try {
            $service->remove($id);
        } catch (BackErrorException $e) {
            return BackResult::error([], $e->getMessage());
        }
        return BackResult::success([]);
    }

    /**
     * 上传字幕文件 不用保存 用完即删 因为要转换成数据库信息
     */
    public function uploadWordScreen(Request $request)
    {
        $file = $request->file("file");
        $videoId = $request->post("videoId");
        $type = $request->post("type");

        if (empty($videoId)) {
            return BackResult::error([], "请选择视频");
        }
        $service = new UploadService();
        $service->uploadWordScreen($file, $videoId, $this->admin['id'], $type);
        return BackResult::success();
    }

    /**
     * 清空当前视频的字幕 不知道这个字幕的id跟收藏有没有关系 有关系的话 一样要清除
     * 清空字幕
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     */
    public function clearWordScreen(Request $request)
    {
        $videoId = $request->param("videoId");
        $service = new VideoService();
        $service->clearWordScreen($videoId, $this->admin['id']);
        return BackResult::success();
    }

    /**
     * 获取视频字幕
     * @param Request $request
     * @return \think\Response
     */
    public function getWordScreen(Request $request)
    {
        $videoId = $request->param('videoId');
        $page = $request->param("page");
        $pageSize = $request->param("pageSize");

        $service = new VideoService();
        if (empty($page)) {
            $wordScreen = $service->getWordScreenNoPage($videoId);
        } else {
            $wordScreen = $service->getWordScreen($videoId, $page, $pageSize);
        }

        return BackResult::success($wordScreen, '操作成功');
    }

    /**
     * 获取指定字幕
     * @param Request $request
     * @return \think\Response
     */
    public function getWordScreenItem(Request $request)
    {
        $id = $request->param("id");
        $service = new VideoService();
        $data = $service->getWordScreenItem($id);
        return BackResult::success($data, '获取成功');
    }

    /**
     * 编辑字幕
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     */
    public function saveWordScreenItem(Request $request)
    {
        $data = $request->post();
        if (empty($data['start'])) {
            return BackResult::error([], '请输入开始时间');
        }
        if (empty($data['end'])) {
            return BackResult::error([], '请输入结束时间');
        }

        $service = new VideoService();
        $data = $service->saveWordScreenItem($data, $this->admin['id']);
        return BackResult::success($data, "保存成功");
    }

    /**
     * 字母提交审核
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     */
    public function supplyWordScreen(Request $request)
    {
        $videoId = $request->param("id");
        $service = new VideoService();
        $service->supplyWordScreen($videoId, $this->admin['id']);
        return BackResult::success();
    }

    /**
     * 完成字幕审核
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     */
    public function sureWordScreen(Request $request)
    {
        $videoId = $request->param("id");
        $service = new VideoService();
        $service->sureWordScreen($videoId, $this->admin);
        return BackResult::success();
    }

    /**
     * 发送字幕
     * @param Request $request
     * @return \think\Response
     * @throws BackErrorException
     */
    public function sendBackWordScreen(Request $request)
    {
        $videoId = $request->param("id");
        $service = new VideoService();
        $service->sendBackWordScreen($videoId, $this->admin);
        return BackResult::success();
    }

    /**
     * @param Request $request
     * @return \think\Response
     */
    public function trans(Request $request)
    {
        $sentence = $request->param("sentence");
        $res = Trans::translate($sentence, 'en', 'zh');
        return BackResult::success($res);
    }

    public function removeWordScreen(Request $request)
    {
        $id = (int)$request->param('id');
        $service = new VideoService();
        $service->deleteWordScreenItem($id, $this->isSuper() ? 0 : $this->admin['id']);
        return BackResult::success();
    }

    public function downloadWordScreen(Request $request)
    {
        if (!$this->isSuper()) {
            return BackResult::error([], '无权操作');
        }
        $id = (int)$request->param('id');
        $service = new VideoService();
        return BackResult::success($service->outputWordScreen($id));
    }
}